package com.solcoo.merchant.ui.reg;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.common.util.ToastUtil;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseActivity;
import com.solcoo.merchant.base.task.TaskHelper;
import com.solcoo.merchant.base.task.TaskHelper.Callback;
import com.solcoo.merchant.base.task.TaskHelper.DefaultCallback;
import com.solcoo.merchant.manager.UserManager;
import com.solcoo.merchant.model.User;
import com.solcoo.merchant.util.CheckUtil;
import com.solcoo.merchant.util.CheckUtil.CheckResult;

public class RegInfoActivity extends BaseActivity {

	private EditText pwd1;
	private EditText pwd2;

	private String phoneNo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reg_info);
		phoneNo = getIntent().getStringExtra("phoneNo");
		Button btn = (Button) findViewById(R.id.send);
		btn.setOnClickListener(sendOnClickListener);
		pwd1 = (EditText) findViewById(R.id.edit_pwd1);
		pwd2 = (EditText) findViewById(R.id.edit_pwd2);
	}

	private OnClickListener sendOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String pwd = pwd1.getText().toString();
			CheckResult checkResult = CheckUtil.checkPwd(pwd, pwd2.getText()
					.toString());
			if (checkResult.isSuccess()) {
				TaskHelper.oper(v.getContext(),
						Constant.URL.getReg(phoneNo, pwd), regCallback,
						User.class);
			} else {
				ToastUtil.showToast(v.getContext(), checkResult.failMsg);
			}
		}

	};

	private Callback<User> regCallback = new DefaultCallback<User>(this) {
		private Dialog mDialog;

		@Override
		public void onSuccess(User result) {
			UserManager.getInstance().setUser(result);
			UserManager.getInstance().loginSuccess(mDialog,
					RegInfoActivity.this, "注册成功");
		}

	};

}
